/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.ipo.mm.ejb.facade.institution;

import java.util.Collection;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import org.ipo.mm.ejb.entities.Institution;
import org.ipo.mm.ejb.util.InstitutionSearchQueryBuilder;

/**
 *
 * @author makcro
 */
@Stateless
public class InstitutionFacade implements InstitutionFacadeLocal, InstitutionFacadeRemote {

    @PersistenceContext
    private EntityManager em;

    public void create(Institution institution) {
        em.persist(institution);
    }

    public void edit(Institution institution) {
        em.merge(institution);
    }

    public void remove(Institution institution) {
        em.remove(em.merge(institution));
    }

    public Institution find(Object id) {
        return em.find(Institution.class, id);
    }

    public List<Institution> findAll() {
        return em.createNamedQuery("Institution.findAll").getResultList();
    }

    public Collection<Institution> search(
            String nameFilter,
            String nameOperator,
            String addressFilter,
            String addressOperator,
            String emailFilter,
            String emailOperator,
            String telephoneFilter,
            String telephoneOperator,
            String faxFilter,
            String faxOperator,
            String representativeFilter,
            String representativeOperator,
            String[] typeFilter,
            String[] regionFilter,
            String[] familyFilter,
            String[] cityFilter,
            String orderBy,
            int firstResult,
            int maxResults,
            boolean isQuickSearch) {

        orderBy = mapOrderBy(orderBy);

        List<Institution> result = null;

        InstitutionSearchQueryBuilder isqb = new InstitutionSearchQueryBuilder(
                nameFilter,
                nameOperator,
                addressFilter,
                addressOperator,
                emailFilter,
                emailOperator,
                telephoneFilter,
                telephoneOperator,
                faxFilter,
                faxOperator,
                representativeFilter,
                representativeOperator,
                typeFilter,
                regionFilter,
                familyFilter,
                cityFilter,
                orderBy,
                false,
                isQuickSearch);

//        String queryS = "SELECT i FROM Institution i JOIN i.representativeCollection r " +
//                " WHERE i.type.id IN (2, 3) " +
//                " AND " +
//                " i.region.id IN (2) " +
//                " AND " +
//                " i.family.id IN (3, 4) " +
//                " AND " +
//                " r.firstName LIKE '%a%'";


        String queryS = isqb.getQuery();


        Query query = em.createQuery(queryS);

        if (firstResult != -1 && maxResults != -1) {
            query.setFirstResult(firstResult);
            query.setMaxResults(maxResults);
        }

        result = query.getResultList();

        return result;


    }

    public Long getSearchResultCount(
            String nameFilter,
            String nameOperator,
            String addressFilter,
            String addressOperator,
            String emailFilter,
            String emailOperator,
            String telephoneFilter,
            String telephoneOperator,
            String faxFilter,
            String faxOperator,
            String representativeFilter,
            String representativeOperator,
            String[] typeFilter,
            String[] regionFilter,
            String[] familyFilter,
            String[] cityFilter,
            String orderBy,
            boolean isQuickSearch) {
        orderBy = mapOrderBy(orderBy);
        InstitutionSearchQueryBuilder isqb = new InstitutionSearchQueryBuilder(
                nameFilter,
                nameOperator,
                addressFilter,
                addressOperator,
                emailFilter,
                emailOperator,
                telephoneFilter,
                telephoneOperator,
                faxFilter,
                faxOperator,
                representativeFilter,
                representativeOperator,
                typeFilter,
                regionFilter,
                familyFilter,
                cityFilter,
                orderBy,
                true,
                isQuickSearch);


        String queryS = isqb.getQuery();

        Query query = em.createQuery(queryS);
        //Vector result = (Vector) query.getSingleResult();

        return (Long) query.getSingleResult();


    }

    private String mapOrderBy(String orderBy){
        if(orderBy != null){
            if (orderBy.equals("1")){
                orderBy = "name";
            }
            else if (orderBy.equals("2")){
                orderBy = "email";
            }
            else if (orderBy.equals("3")){
                orderBy = "address";
            }
            else if (orderBy.equals("4")){
                orderBy = "telephone";
            }
            else if (orderBy.equals("5")){
                orderBy = "fax";
            }
            else if("6".equals(orderBy)){
                orderBy = "type.name";
            }
            else if("7".equals(orderBy)){
                orderBy = "family.name";
            }
            else if("8".equals(orderBy)){
                orderBy = "region.name";
            }
            else if("9".equals(orderBy)){
                orderBy = "city.name";
            }

        }
        return orderBy;
    }

}
